﻿using Enring.Global;
using System;

namespace Enring.Web.Install
{
    public partial class Step1 : Installer
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {
            btnTest.Click += new EventHandler(btnTest_Click);
            btnCreate.Click += new EventHandler(btnCreate_Click);
        }

        void btnCreate_Click(object sender, EventArgs e)
        {
            using (SqlConnectionInfo SqlInfo = new SqlConnectionInfo())
            {
                SqlInfo.DataSource = txtServer.Text;
                SqlInfo.UserID = txtUser.Text;
                SqlInfo.Password = txtPass.Text;
                SqlInfo.InitialCatalog = txtDatabase.Text;
                if (!ModifyWebConfig(SqlInfo))
                {
                    labMess.Text = "修改配置文件失败！";
                }
                else
                    Response.Redirect("step2.aspx");
            }
        }

        void btnTest_Click(object sender, EventArgs e)
        {
            if (txtDatabase.Text.Trim() == string.Empty | txtServer.Text.Trim() == string.Empty)
            {
                labMess.Text = "信息不完整";
                return;
            }
            using (SqlConnectionInfo SqlInfo = new SqlConnectionInfo())
            {
                SqlInfo.DataSource = txtServer.Text;
                SqlInfo.UserID = txtUser.Text;
                SqlInfo.Password = txtPass.Text;
                SqlInfo.InitialCatalog = txtDatabase.Text;

                ConnectionResult rst = SqlInfo.TestConnection();
                if (rst == ConnectionResult.JustServerOK)
                {
                    labMess.Text = "测试成功";
                    btnCreate.Enabled = true;
                }
                else if (rst == ConnectionResult.ExistsDatabase)
                    labMess.Text = "数据库已存在，请指定其它数据库名.";
                else
                    labMess.Text = "连接失败，请重试.";
            }
               
        }
    }
}